package com.woniuxy.cloud.carl.carlstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.carl.carlstation.infra.mapper.StaffMapper;
import com.woniuxy.cloud.carl.carlstation.infra.model.Staff;
import com.woniuxy.cloud.carl.carlstation.service.StaffService;
import com.woniuxy.cloud.carl.carlstation.service.dto.StaffServiceDto;
import com.woniuxy.result.Result;
import com.woniuxy.utils.MyPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工管理 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {

    @Resource
    private  StaffMapper staffMapper;
    @Override
    public  MyPage<StaffServiceDto> findStaffByCondition(Integer pageNum, Integer pageSize, String condition) {
        if(pageNum==null||pageSize==null){
            pageNum=1;
            pageSize=3;
        }
        Page<Staff> page = new Page<>(pageNum, pageSize);
        MyPage<StaffServiceDto> myPage= new MyPage<>();
        if(condition==null||condition.equals("")){

            staffMapper.selectPage(page,null);
        }else {

            QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("staff_name",condition)
                    .or().like("phone",condition);

            staffMapper.selectPage(page,queryWrapper);
        }
        System.out.println(page.toString());
        List<StaffServiceDto> staffServiceDtos = new ArrayList<>();

        for (Staff staff : page.getRecords()) {

            StaffServiceDto staffServiceDto = BeanUtil.toBean(staff, StaffServiceDto.class);

            if(staffServiceDto.getState()==1){
                staffServiceDto.setStateName("工作中");
            }else if(staffServiceDto.getState()==2){
                staffServiceDto.setStateName("空闲");
            }else if(staffServiceDto.getState()==3){
                staffServiceDto.setStateName("休假中");
            }else {
                staffServiceDto.setStateName("离职");
            }
            staffServiceDtos.add(staffServiceDto);
        }
        myPage.setPages(page.getPages());
        myPage.setPageNum(page.getCurrent());
        myPage.setTatol(page.getTotal());
        myPage.setPageSize(page.getSize());
        myPage.setList(staffServiceDtos);
        System.out.println(myPage.getList());
        return myPage;
    }

    @Override
    public Staff checkStaffByPhone(String managerPhone) {

        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",managerPhone);
        Staff staff = this.getOne(queryWrapper);

        return staff;
    }
}
